<?php
class Alidayusms 
{
    private $appkey  = '23581440';
    private $secret  = '093f60a1db570bbbbdd4c8a070ff3c9f';
    private $smsType = 'normal';
    private $signName = '贝竹';
    
    public function __construct()
    {
        include 'TopSdk.php';
        date_default_timezone_set('Asia/Shanghai');
    }
    
    /**
     * 发送短信
     * @param unknown $recNum 对应手机号码
     * @param json $smsParam 对应模版变量名称
     * @param string $smsTemplateCode 对应阿里大于模版ID
     * @param string $extend 公共回传参数，在“消息返回”中会透传回该参数
     */
    public function smsNumSend($recNum, $smsParam, $smsTemplateCode, $extend='')
    {
        $c = new TopClient;
        $c->appkey = $this->appkey ;
        $c->secretKey = $this->secret ;
        $req = new AlibabaAliqinFcSmsNumSendRequest;
        $req->setExtend($extend);
        $req->setSmsType($this->smsType);
        $req->setSmsFreeSignName($this->signName);
        $req->setSmsParam($smsParam);
        $req->setRecNum($recNum);
        $req->setSmsTemplateCode($smsTemplateCode);
        $response = $c->execute($req);
        return $this->formatRes($response);
    }
    
    /**
     * 短信发送记录查询
     * @param unknown $recNum 对应手机号码
     * @param unknown $queryDate 查询日期
     * @param number $currentPage 当前页
     * @param number $pageSize 每页显示条数
     */
    public function smsNumQuery($recNum, $queryDate, $currentPage=1, $pageSize = 10)
    {
        $c = new TopClient;
        $c ->appkey = $this->appkey ;
        $c ->secretKey = $this->secret ;
        $req = new AlibabaAliqinFcSmsNumQueryRequest;
        $req ->setBizId('1234^1234');
        $req ->setRecNum($recNum);
        $req ->setQueryDate($queryDate);
        $req ->setCurrentPage($currentPage);
        $req ->setPageSize($pageSize);
        $response = $c->execute($req);
        return $this->formatRes($response);
    }
    
    /**
     * 验证码  - (文本转语音) 接口详情
     * @param unknown $calledNum 被呼叫手机号码
     * @param json $ttsParam 对应模版变量名称
     * @param string $ttsCode  对应阿里大于模版ID
     * @param string $calledShowNum 阿里大于呼出的手机号
     * @param string $extend  公共回传参数，在“消息返回”中会透传回该参数
     * @return Ambigous <multitype:, string, array>
     */
    public function ttsNumSinglecall($calledNum, $ttsParam, $ttsCode, $calledShowNum='40012341234', $extend='')
    {
        $c = new TopClient;
        $c ->appkey = $this->appkey ;
        $c ->secretKey = $this->secret ;
        $req = new AlibabaAliqinFcTtsNumSinglecallRequest;
        $req ->setExtend($extend);
        $req ->setTtsParam($ttsParam);
        $req ->setCalledNum($calledNum);
        $req ->setCalledShowNum($calledShowNum);
        $req ->setTtsCode($ttsCode);
        $response = $c ->execute( $req );
        return $this->formatRes($response);
    }
    
    /**
     * 语音通知  -  (语音通知) 接口详情
     * @param unknown $calledNum 被呼叫手机号码
     * @param string $voiceCode  对应阿里大于语音文件
     * @param string $calledShowNum 阿里大于呼出的手机号
     * @param string $extend  公共回传参数，在“消息返回”中会透传回该参数
     * @return Ambigous <multitype:, string, array>
     */
    public function voiceNumSinglecall($calledNum, $voiceCode, $calledShowNum='40012341234', $extend='')
    {
        $c = new TopClient;
        $c ->appkey = $this->appkey ;
        $c ->secretKey = $this->secret ;
        $req = new AlibabaAliqinFcVoiceNumSinglecallRequest;
        $req ->setExtend($extend);
        $req ->setCalledNum($calledNum);
        $req ->setCalledShowNum($calledShowNum);
        $req ->setVoiceCode($voiceCode);
        $response = $c ->execute( $req );
        return $this->formatRes($response);
    }
    
    private function formatRes($response)
    {
        $dataAry = array();
        
        $xmlAry = (array)$response;
        if (isset($xmlAry['sub_code'])) {
            $xmlAry['success'] = 'false';
            $dataAry = $xmlAry;
        }
        if (isset($xmlAry['result'])) {
            $xmlAry = (array)$xmlAry['result'];
            $dataAry = $xmlAry;
        }
        return $dataAry;
    }
}